//
//  WBOriginalView.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class WBOriginalView: WBBaseView {
    
    /// 创建子控件
    /// 用户头像
    let iconImageView: UIImageView = UIImageView(imageName: "avatar_default_big")
    
    /// 用户的vip图标
    let vipIcon: UIImageView = UIImageView(imageName: "avatar_vip")
    
    /// 用户名
    let userNameLabel: UILabel = UILabel(title: "JayHua", fontSize: 15)
    
    /// 用户等级
    let levelIcon: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    
    /// 创建日期
    let createTimeLabel: UILabel = UILabel(title: "昨天", fontSize: 10, textColor: UIColor.lightGray)
    
    /// 来源
    let souceLabel: UILabel = UILabel(title: "07微博", fontSize: 10, textColor: UIColor.lightGray)
    
    /// 微博正文
    let statusLabel: UILabel = UILabel(title: "JayHua", fontSize: 15)
    
    /// 配图视图
    let statusPictureView: WBHomePictureView = WBHomePictureView()
    
    /// viewModel
    var statusViewModel: WBHomeViewModel? {
        didSet {
            statusPictureView.statusViewModel = statusViewModel
            
            //异步绘制圆角图片
            SDWebImageManager.shared().downloadImage(with: URL(string: (statusViewModel?.statusModel.user?.avatar_large)!), options: [], progress: nil) { (downloadImage, _, _, _, _) in
                
                downloadImage?.createCircleImage(size: CGSize(width: 35, height: 35), callBack: { (circleImage) in
                    self.iconImageView.image = circleImage
                })
                
            }

            // 设置 用户名
            userNameLabel.text = (statusViewModel?.statusModel.user?.screen_name)!
            
            // 设置微博正文文字
            statusLabel.text = statusViewModel?.statusModel.text
            
            // 设置vip图标
            vipIcon.image = statusViewModel?.vipImage
            
            // 设置皇冠等级
            levelIcon.image = statusViewModel?.levelImage
            
            // 设置来源
            souceLabel.text = statusViewModel?.sourceText
            
            // 设置时间
            createTimeLabel.text = statusViewModel?.timeText
            
            //判断是否有图片
            if let pic_urls = statusViewModel?.statusModel.pic_urls, pic_urls.count > 0 {
                //如果有图片, 设置图片的顶部margin和图片的size
                statusPictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(statusLabel.snp.bottom).offset(12)
                    make.size.equalTo((statusViewModel?.pictureViewSize)!)
                })
            }else{
                //如果没有有图片, 将图片的顶部margin和图片的size都设为0
                statusPictureView.snp.updateConstraints({ (make) in
                    make.size.equalTo(CGSize.zero)
                    make.top.equalTo(statusLabel.snp.bottom)
                })
            }
        }
    }
    
    override func wbSetupSubviews() {
        super.wbSetupSubviews()
        self.setupUI()
    }

}

// MARK: - 设置UI
extension WBOriginalView {
    //1. 从上往下, 从左往右, 依次布局
    //2. 如果控件比较多, 布局完成一个, 就看一看效果
    fileprivate func setupUI () {
        //设置背景色
        backgroundColor = UIColor.white
        
        // 添加子视图
        addSubview(iconImageView)
        addSubview(vipIcon)
        addSubview(userNameLabel)
        addSubview(levelIcon)
        addSubview(createTimeLabel)
        addSubview(souceLabel)
        addSubview(statusLabel)
        addSubview(statusPictureView)
        
        // 自动布局
        iconImageView.snp.makeConstraints { (makt) in
            makt.size.equalTo(statusLayout.iconSize)
            makt.top.left.equalTo(self).offset(statusLayout.margin)
        }
        
        vipIcon.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(-7)
            make.top.equalTo(iconImageView.snp.bottom).offset(-10)
            make.size.equalTo(statusLayout.littleIconSize)
        }
        
        userNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp.right).offset(statusLayout.margin)
        }
        
        levelIcon.snp.makeConstraints { (make) in
            make.left.equalTo(userNameLabel.snp.right).offset(7)
            make.bottom.equalTo(userNameLabel).offset(-3)
            make.size.equalTo(statusLayout.littleIconSize)
        }
        
        createTimeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp.right).offset(statusLayout.margin)
        }
        
        souceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconImageView)
            make.left.equalTo(createTimeLabel.snp.right).offset(statusLayout.margin)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView)
            make.right.equalTo(self).offset(-statusLayout.margin)
            make.top.equalTo(iconImageView.snp.bottom).offset(statusLayout.margin)
        }
        
        //配图视图的自动布局
        statusPictureView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.zero)
            make.left.equalTo(statusLabel)
            make.top.equalTo(statusLabel.snp.bottom).offset(statusLayout.margin)
            //让最下面的子视图和父视图片bottom绑定
            make.bottom.equalTo(self).offset(-statusLayout.margin)
        }
        
    }
}
